package com.cs.client;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.PathChildrenCache;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent;
import org.apache.curator.framework.recipes.cache.PathChildrenCacheListener;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ClientBootStrap {


    //创建RPC连接线程池对象
    public static ExecutorService executor = Executors.newFixedThreadPool(2);
    //zookeeper客户端
    static CuratorFramework curator ;
    //服务集合
    static ConcurrentHashMap<String,String> serverMap = new ConcurrentHashMap<>();
    private static String path = "/server";//服务节点根路径
    public static void main(String[] args) throws Exception {

        curator = ZookeeperConfig.getCuratorFramework();//初始化zookeeper客户端


        watch(curator,path);//初始化服务集合serverMap并监听服务器信息变化

        while (true) {
            Thread.sleep(5000);
            //启动服务连接线程（测试用）
            executor.submit(new RpcThread());
        }
    }


    /**
     *
     * 监听节点变化
     *
     * */
    public static void watch(CuratorFramework curator,String path)throws Exception{


        //子节点添加watcher
        //childData：设置缓存节点的数据状态
        final PathChildrenCache childrenCache = new PathChildrenCache(curator,path,true);

        /*
         * StartMode：初始化方式
         * POST_INITIALIZED_EVENT：异步初始化。初始化后触发事件
         * NORMAL：异步初始化
         * BUILD_INITIAL_CACHE：同步初始化
         * */
        childrenCache.start(PathChildrenCache.StartMode.NORMAL);

        childrenCache.getListenable().addListener(new PathChildrenCacheListener() {
            public void childEvent(CuratorFramework ient, PathChildrenCacheEvent event) throws Exception {
                if(event.getType().equals(PathChildrenCacheEvent.Type.INITIALIZED)){//POST_INITIALIZED_EVENT：异步初始化。初始化后触发事件
                    System.out.println("子节点初始化成功");
                }else if(event.getType().equals(PathChildrenCacheEvent.Type.CHILD_ADDED)){//添加子节点
                    System.out.println("添加子节点"+event.getData().getPath()+":::"+new String(event.getData().getData()));
                    //载入服务集合
                    serverMap.put(event.getData().getPath(),new String(event.getData().getData()));//节点与节点信息为一个map


                }else if(event.getType().equals(PathChildrenCacheEvent.Type.CHILD_REMOVED)){//删除子节点
                    System.out.println("删除子节点"+event.getData().getPath());
                    //删除对应服务
                    serverMap.remove(event.getData().getPath());//节点与节点信息为一个map

                }else if(event.getType().equals(PathChildrenCacheEvent.Type.CHILD_UPDATED)){//修改子节点
                    System.out.println("添加子节点"+event.getData().getPath()+":::"+new String(event.getData().getData()));
                    //载入服务集合
                    serverMap.put(event.getData().getPath(),new String(event.getData().getData()));//节点与节点信息为一个map
                }
            }
        });
    }





}
